package com.neudu.yiyang.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neudu.yiyang.commons.JSONReturn;
import com.neudu.yiyang.pojo.Customer;
import com.neudu.yiyang.pojo.Outward;
import com.neudu.yiyang.service.CustomerService;
import com.neudu.yiyang.service.OutwardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@RestController
public class OutwardController {
    @Autowired
    private OutwardService outwardService;

    @Autowired
    private CustomerService customerService;
    @Autowired
    private JSONReturn jsonReturn;

    // 查询所有外出记录（分页）
    @PostMapping("get_all_outward")
    public String getAllOutward(@RequestParam("pageNum") Integer pageNum,
                                @RequestParam("pageSize") Integer pageSize,
                                @RequestParam(required = false) String keyword) {
        try {
            Page<Outward> page = new Page<>(pageNum, pageSize);
            Map<String, Object> result = outwardService.getOutwardWithCustomerName(page, keyword);
            return jsonReturn.returnSuccess(result);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("get_outward_by_id")
    public String getById(@RequestParam("id") Integer id) {
        try {
            if (id == null) {
                return jsonReturn.returnError("ID不能为空");
            }
            Outward outward = outwardService.getById(id);
            return jsonReturn.returnSuccess(outward);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    @RequestMapping("add_one_outward")
    public String addOne(@RequestBody Outward outward) {
        try {
            Integer customerId = outward.getCustomerId();
            if (customerId == null) {
                return jsonReturn.returnError("客户ID不能为空");
            }

            Customer customer = customerService.getById(customerId);
            if (customer == null) {
                return jsonReturn.returnError("客户ID在客户表中不存在");
            }

            // 检查客户状态是否为已退住
            if (customer.getStatus() != null && customer.getStatus() == 1) {
                return jsonReturn.returnError("该客户已退住，无法新增外出登记");
            }

            outward.setIsDeleted(0);
            outward.setAuditstatus(0);
            boolean success = outwardService.save(outward);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("新增失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
    @RequestMapping("modify_outward_by_id")
    public String modifyById(@RequestBody Outward outward) {
        try {
            if (outward.getId() == null) {
                return jsonReturn.returnError("ID不能为空");
            }
            boolean success = outwardService.updateById(outward);
            if (success) {
                return jsonReturn.returnSuccess(outward);
            } else {
                return jsonReturn.returnError("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @RequestMapping("remove_outward_by_id")
    public String removeById(@RequestParam("id") Integer id) {
        try {
            if (id == null) {
                return jsonReturn.returnError("ID不能为空");
            }
            boolean success = outwardService.removeById(id);
            if (success) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}